package com.example.xinli.controller;

import com.example.xinli.domain.vo.MoodDiaryVo;
import com.example.xinli.domain.vo.ResponseBean;
import com.example.xinli.service.IInterService;
import com.example.xinli.service.IMoodDiaryService;
import com.example.xinli.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
@RequestMapping("/moodDiary")
public class MoodDiaryController {

    @Autowired
    private IMoodDiaryService moodDiaryService;

    // 获取一个用户的所有心情日记
    @RequestMapping("/getAllMoodDiary")
    @ResponseBody
    public ResponseBean getMoodDiaryByToken(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        String userId = TokenUtil.getId(token);
        return moodDiaryService.getMoodDiaryByUserId(userId);
    }

    // 添加一条心情日记
    @RequestMapping("/addMoodDiary")
    @ResponseBody
    public ResponseBean addMoodDiary(@RequestBody MoodDiaryVo moodDiaryVo) {
        return moodDiaryService.addMoodDiary(moodDiaryVo);
    }

    // 删除一条心情日记
    @RequestMapping("/removeMoodDiary")
    @ResponseBody
    public ResponseBean removeMoodDiary(@RequestParam("diaryId") int diaryId) {
        return moodDiaryService.removeMoodDiary(diaryId);
    }

    // 编辑一条心情日记
    @RequestMapping("/editMoodDiary")
    @ResponseBody
    public ResponseBean editMoodDiary(@RequestBody MoodDiaryVo moodDiaryVo) {
        return moodDiaryService.editMoodDiary(moodDiaryVo);
    }
}

